package com.lucker.service.caipiao;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import com.lucker.cds.mapper.CCaiPiaoOpencodeMapper;
import com.lucker.cds.model.CCaiPiaoOpenTime;
import com.lucker.cds.model.CCaiPiaoOpencode;
import com.lucker.cds.model.CEvenyDayOpenTime;
import com.lucker.common.util.DateTimeUtil;
import com.lucker.ds.model.CaiPiao;
import com.lucker.ds.model.CaiPiaoOpenTime;
import com.lucker.ds.model.CaiPiaoOpencode;
import com.lucker.framework.constant.ResponseBaseModel;
import com.lucker.service.BaseServiceImpl;
import com.lucker.service.evenydayopentime.EvenyDayOpenTimeService;

@Service
public class CaiPiaoOpencodeServiceImpl extends BaseServiceImpl<CaiPiaoOpencode, CCaiPiaoOpencode, Long> implements CaiPiaoOpencodeService {
	
	@Autowired
	private CaiPiaoService caiPiaoService;
	
	@Autowired
	private CaiPiaoOpenTimeService caiPiaoOpenTimeService;
	
	@Autowired
	private EvenyDayOpenTimeService evenyDayOpenTimeService;
	
	@Override
	public CCaiPiaoOpencodeMapper getCustomMapper() {
		return (CCaiPiaoOpencodeMapper)this.getBaseCustomMapper();
	}

	
	//CCaiPiaoOpencodeMapper customSysCheckCodeMapper = (CCaiPiaoOpencodeMapper)this.getBaseCustomMapper();
	
	/*
	
	@Transactional
	public void initSSQ(String caipiaoid, int maxPageIndex) {
		int pageSize = 10;
	    if(maxPageIndex<1)return;
		for(int pageIndex = 0 ; pageIndex < maxPageIndex ; pageIndex++) {
			int startRowIndex = pageIndex * pageSize;
			if(StringUtil.isBlank(caipiaoid))caipiaoid = "11";
			String _cp_history_opencode_json = null;
			try {
				_cp_history_opencode_json = JiSuDataUtil.getHistoryOpenCaiPiaoInfo(caipiaoid, null, pageSize, startRowIndex);
				if(StringUtil.isJsonString(_cp_history_opencode_json)) {
					System.out.println(_cp_history_opencode_json);
					JSONObject _cp_history_opencode_object = JSONObject.parseObject(_cp_history_opencode_json);
					if(_cp_history_opencode_object != null && _cp_history_opencode_object.containsKey("status") && _cp_history_opencode_object.containsKey("result") && _cp_history_opencode_object.getInteger("status") == 0) {
						JSONObject _cp_opencode_object = _cp_history_opencode_object.getJSONObject("result");
						if(_cp_opencode_object != null && _cp_opencode_object.containsKey("list")) {
							JSONArray _openCodeArray = _cp_opencode_object.getJSONArray("list");
							for(int i = 0 ; _openCodeArray != null && i < _openCodeArray.size() ; i++) {
								JSONObject _cp_openCode = _openCodeArray.getJSONObject(i);
								CaiPiaoOpencode _cp_openCode_object = getSSQObjectByJson(_cp_openCode);
								if(_cp_openCode_object != null) {
									_cp_openCode_object.setCaiId(_cp_opencode_object.getString("caipiaoid"));
									CaiPiaoOpencode _queryParam = new CaiPiaoOpencode();
									_queryParam.setCaiId(_cp_openCode_object.getCaiId());
									_queryParam.setIssueno(_cp_openCode_object.getIssueno());
									if(this.queryAllCount(_queryParam) < 1) {
										CaiPiao query_cp = new CaiPiao();
										//query_cp.setCaiId(_cp_openCode_object.getCaiId());
										List<CaiPiao> cplist = caiPiaoService.queryAll(query_cp);
										if(cplist != null && cplist.size() > 0) {
											_cp_openCode_object.setCaiName(cplist.get(0).getCaiName());
										}
										this.add(_cp_openCode_object, false);
									}
								}								
							}
						}
					}else {
						getLOGGER().error("获取彩票历史开奖信息错误 , 接口返回值异常： {}", _cp_history_opencode_object.toJSONString());
						throw new BaseServiceException("获取彩票历史开奖信息错误 , 接口返回值异常");
					}
				}
				
			}catch (Exception e) {
				getLOGGER().error("获取彩票历史开奖信息错误 : {} , {}",e,e.getMessage());
				throw new BaseServiceException("获取彩票历史开奖信息错误",e);
			}
			
		}
    }

	public CaiPiaoOpencode getSSQObjectByJson(JSONObject o) {
		CaiPiaoOpencode _cp_openCode = new CaiPiaoOpencode();
		String _number = o.getString("number");
		if(StringUtil.isNotBlank(_number))_number = _number.replaceAll(" ", "-");
		_cp_openCode.setOpendate(o.getDate("opendate"));
		_cp_openCode.setIssueno(o.getString("issueno"));
		_cp_openCode.setNumber(_number);
		_cp_openCode.setRefernumber(o.getString("refernumber"));
		_cp_openCode.setSaleamount(o.getBigDecimal("saleamount"));
		_cp_openCode.setPrize(o.getString("prize"));
		return _cp_openCode;
	}
	


  */
	
	
//	
//	public CaiPiaoOpenTime getNextOpenCodeTime(Long caiId) {
//		CCaiPiaoOpenTime cquery = new CCaiPiaoOpenTime();
//		cquery.setOpenTime(DateTimeUtil.getDate(new Date(), "yyyy-MM-dd"));
//		cquery.setCaiId(caiId);
//		List<CCaiPiaoOpenTime> clist = caiPiaoOpenTimeService.getCustomMapper().selectNextOpenTime(cquery);
//		if(CollectionUtils.isNotEmpty(clist)) {
//			
//		}else {
//			
//		}
//		return null;
//	}
//	
	
	
	
	
	
	
	
	
	public CaiPiaoOpenTime getNextOpenCodeTime(Long caiID) {
		//CaiPiaoOpenTime nextOpenTime = new CaiPiaoOpenTime();
		CEvenyDayOpenTime cEvenyDayOpenTime = new CEvenyDayOpenTime();
		cEvenyDayOpenTime.setCaiId(caiID);
		CEvenyDayOpenTime day = evenyDayOpenTimeService.getCEvenyDayOpenTime(cEvenyDayOpenTime);
		if(day != null) {
			CaiPiaoOpenTime nextOpenTime = new CaiPiaoOpenTime();
			nextOpenTime.setOpenTime(String.valueOf(day.getOpenDate().getTime()));
			nextOpenTime.setCaipiaoIssueno(day.getIssueno());
			return nextOpenTime;
		}
		return null;
		
		
//		if(StringUtils.isBlank(issno)){
//			return null;
//		}
//		String issnoDay = null;
//		if(issno.length() == 8) {
//			issnoDay = StringUtils.substring(issno, 0 , 6);
//			issno = StringUtils.substring(issno, issno.length() - 2, issno.length());
//			int nextIssno = Integer.parseInt(issno) + 1;
//			if(nextIssno < 10) {
//				issno = "0" + nextIssno;
//			}else {
//				issno = "" + nextIssno;
//			}
//		}
//		CaiPiaoOpenTime nextOpenTime = getOpenCodeTime(issno,caiID);
//		String qino = issno;
//		if(nextOpenTime == null) {
//			qino = "01";
//			nextOpenTime = getOpenCodeTime(qino,caiID);
//		}
//		
//		if(nextOpenTime != null) {
//			String nextLastTime = nextOpenTime.getOpenTime();
//			try {
//				Date openTime = DateTimeUtil.getDateByString(issnoDay + " " +  nextLastTime ,"yyMMdd HH:mm:ss" );
//			    if(openTime.getTime() < new Date().getTime()) { //下一期开奖时间小于当前时间，则说明年月日应该设置为明天的年月日
//			    	openTime = DateTimeUtil.addDay(openTime, 1);
//			    	issnoDay =  DateTimeUtil.getDate(openTime,"yyMMdd");
//			    }
//			    nextOpenTime.setOpenTime(String.valueOf(openTime.getTime()));
//			    nextOpenTime.setCaipiaoIssueno( issnoDay+qino );
//			    return nextOpenTime;
//			}catch (Exception e) {
//				throw new BaseServiceException("下一期开奖时间计算异常") ;
//			}
//		}
		
	}
	
	
	public CaiPiaoOpenTime getOpenCodeTime(String issno,Long caiID) {
		CaiPiaoOpenTime query = new CaiPiaoOpenTime();
		query.setCaiId(caiID);
		query.setCaipiaoIssueno(issno);
		List<CaiPiaoOpenTime> list = caiPiaoOpenTimeService.queryAll(query);
		if(CollectionUtils.isNotEmpty(list)) {
			return list.get(0);
		}
		return null;
	}
	
	
	
	public ResponseEntity<?> missCodeCommit(String openCode,String issueNo,String caiId,String openTime){
		CaiPiaoOpencode  queryCaiPiaoOpenCode = new CaiPiaoOpencode();
		queryCaiPiaoOpenCode.setCaiId(caiId);
		queryCaiPiaoOpenCode.setIssueno(issueNo);
		int rows = queryAllCount(queryCaiPiaoOpenCode);
		
		if( rows > 0 ) {
			return new ResponseEntity<>(new ResponseBaseModel(29002,"该期开奖号码已经存在 ！"), HttpStatus.OK);
		}else {
			try {
				queryCaiPiaoOpenCode.setOpendate(DateTimeUtil.getDateByString(openTime, DateTimeUtil.FORMAT_LONGTIME));
			}catch (Exception e) {}
			String openCodeArray[] = openCode.split(",");
			if(openCodeArray != null && openCodeArray.length > 0 && StringUtils.isNotBlank(openCodeArray[0])) {
				queryCaiPiaoOpenCode.setNumber1(openCodeArray[0]);
			}
			if(openCodeArray != null && openCodeArray.length > 1 && StringUtils.isNotBlank(openCodeArray[1])) {
				queryCaiPiaoOpenCode.setNumber2(openCodeArray[1]);	
			}
			if(openCodeArray != null && openCodeArray.length > 2 && StringUtils.isNotBlank(openCodeArray[2])) {
				queryCaiPiaoOpenCode.setNumber3(openCodeArray[2]);
			}
			if(openCodeArray != null && openCodeArray.length > 3 && StringUtils.isNotBlank(openCodeArray[3])) {
				queryCaiPiaoOpenCode.setNumber4(openCodeArray[3]);
			}
			if(openCodeArray != null && openCodeArray.length > 4 && StringUtils.isNotBlank(openCodeArray[4])) {
				queryCaiPiaoOpenCode.setNumber5(openCodeArray[4]);
			}
			if(openCodeArray != null && openCodeArray.length > 5 &&  StringUtils.isNotBlank(openCodeArray[5])) {
				queryCaiPiaoOpenCode.setNumber6(openCodeArray[5]);
			}
			if(openCodeArray != null && openCodeArray.length > 6 && StringUtils.isNotBlank(openCodeArray[6])) {
				queryCaiPiaoOpenCode.setNumber7(openCodeArray[6]);
			}
			if(openCodeArray != null && openCodeArray.length > 7 && StringUtils.isNotBlank(openCodeArray[7])) {
				queryCaiPiaoOpenCode.setNumber8(openCodeArray[7]);
			}
			
			CaiPiao queryCaiPiao = new CaiPiao();
			queryCaiPiao.setCaiId(Integer.parseInt(caiId));
			List<CaiPiao> caiPiaoList = caiPiaoService.queryAll(queryCaiPiao);
			if(CollectionUtils.isNotEmpty(caiPiaoList)) {
				queryCaiPiaoOpenCode.setCaiName(caiPiaoList.get(0).getCaiName());
				queryCaiPiaoOpenCode.setCaiTypeId(caiPiaoList.get(0).getCaiTypeId());
			}
			add(queryCaiPiaoOpenCode, false);
			//向客户端推送
			caiPiaoService.update_push_opencode(queryCaiPiaoOpenCode);
			
			return new ResponseEntity<>(new ResponseBaseModel(10000,"ok"), HttpStatus.OK);
		}
	}
	
	
	
	
}
